package com.icss.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.icss.beans.CourseVO;
import com.icss.beans.HomeWorkVO;
import com.icss.beans.StuClassVO;
import com.icss.beans.User;
import com.icss.daos.HomeWorkDao;

public class HomeWorkServlet extends HttpServlet {

	private String webTempPath;
	public HomeWorkServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action=request.getParameter("action");
		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("User");
		HomeWorkDao hwdao = new HomeWorkDao();
		if("showPersonHomeWork".equals(action)){
			request.getRequestDispatcher("/page/homeWork/homeWork.jsp").forward(request, response);
		}if("showDir".equals(action)){
			List<HomeWorkVO> list = hwdao.getDIRName(u.getStuClassId(),0);
			request.setAttribute("exList",list);
			request.getRequestDispatcher("/page/homeWork/left.jsp").forward(request, response);
		
		
		}else if("folder".equals(action)){
		
			request.getRequestDispatcher("/page/homeWork/folder.jsp").forward(request, response);
		}if("ex_showDir".equals(action)){
			List<StuClassVO> list = hwdao.getStuClass();
			
			request.setAttribute("classList", list);
			request.getRequestDispatcher("/page/homeWork/ex_left.jsp").forward(request, response);
		}else if("ex_folder".equals(action)){
			int classID = Integer.parseInt(request.getParameter("classID"));
			if(classID==0){
				request.setAttribute("sign", "no");
				request.setAttribute("className", "");
			}else{
				String className = hwdao.getclassName(classID);
				List<CourseVO> list = hwdao.getCourseDIR(classID);
				request.setAttribute("className", className);
				request.setAttribute("Courselist", list);
				request.setAttribute("classID", classID);
			}
			
			request.getRequestDispatcher("/page/homeWork/ex_folder.jsp").forward(request, response);
		}else if("displayCourse".equals(action)){
			int courseID = Integer.parseInt(request.getParameter("courseID"));
			List<HomeWorkVO> list = hwdao.getDIRName(u.getStuClassId(),courseID);
			String sign="";
			if(list.size()==0){
				sign="no";
			}else{
				
				sign="yes";
				request.setAttribute("HomeWorkList", list);
				request.setAttribute("fName", hwdao.getCourseName(courseID));
			}
			request.setAttribute("courseID", courseID);
			request.getRequestDispatcher("/page/homeWork/displayCourse.jsp").forward(request, response);
		}else if("toNewEXDIR".equals(action)){
			int courseID = Integer.parseInt(request.getParameter("courseID"));
			request.setAttribute("courseID", courseID);
			String courseName = hwdao.getCourseName(courseID);
			request.setAttribute("courseName", courseName);
			request.getRequestDispatcher("/page/homeWork/newCourseDIR.jsp").forward(request, response);
		}else if("newCourseDIR".equals(action)){
			int courseID = Integer.parseInt(request.getParameter("courseID"));
			String exName = request.getParameter("fileName");
			
			HomeWorkVO vo = new HomeWorkVO();
			vo.setExName(exName);

			vo.setParentID(courseID);
			vo.setStuClassID(u.getStuClassId());
			vo.setUid(u.getUid());
			request.setAttribute("courseID", courseID);
			String courseName = hwdao.getCourseName(courseID);
			request.setAttribute("courseName", courseName);
			
			hwdao.addHomeWorkDIR(vo);
			request.getRequestDispatcher("homeWorkServlet?action=displayCourse&courseID="+courseID).forward(request, response);
		}else if("toNewCoureDIR".equals(action)){
			int courseID = Integer.parseInt(request.getParameter("courseID"));
			int type = Integer.parseInt(request.getParameter("type"));
			if(type==1){
				String className = request.getParameter("n");
				int stuClassID = hwdao.getStuClassID(courseID);
				request.setAttribute("courseName", className);
				request.setAttribute("sign", "y");
				request.setAttribute("stuClassID", stuClassID);
			}else{
				request.setAttribute("courseID", courseID);
				String courseName = hwdao.getCourseName(courseID);
				request.setAttribute("courseName", courseName);
				request.setAttribute("sign", "n");
			}
			
			request.getRequestDispatcher("/page/homeWork/newCourseDIR.jsp").forward(request, response);
		}else if("newCourseDIR2".equals(action)){
			int stuClassID = Integer.parseInt(request.getParameter("stuClassID"));
			String exName = request.getParameter("fileName");
			
			
			request.setAttribute("courseName", hwdao.getclassName(stuClassID));
			
			hwdao.addHomeWorkDIR2(stuClassID,exName);
			request.getRequestDispatcher("homeWorkServlet?action=ex_folder&classID="+stuClassID).forward(request, response);
		}
		hwdao.close();
		
	}

	
	public void init() throws ServletException {
		webTempPath = getServletContext().getRealPath("/") + "upload//homeWork//";
	}

}
